#include "WM.h"
#include "BUTTON.h"
#include "LISTVIEW.h"

#include "WinManager.h"
#include "MENU.h"
#include "FRAMEWIN.h"


#define ID_MENU             (GUI_ID_USER +  0)
#define ID_MENU_FILE_NEW    (GUI_ID_USER +  1)
#define ID_MENU_FILE_OPEN   (GUI_ID_USER +  2)
#define ID_MENU_FILE_CLOSE  (GUI_ID_USER +  3)
#define ID_MENU_FILE_EXIT   (GUI_ID_USER +  4)
#define ID_MENU_FILE_RECENT (GUI_ID_USER +  5)
#define ID_MENU_RECENT_0    (GUI_ID_USER +  6)
#define ID_MENU_RECENT_1    (GUI_ID_USER +  7)
#define ID_MENU_RECENT_2    (GUI_ID_USER +  8)
#define ID_MENU_RECENT_3    (GUI_ID_USER +  9)
#define ID_MENU_EDIT_UNDO   (GUI_ID_USER + 10)
#define ID_MENU_EDIT_REDO   (GUI_ID_USER + 11)
#define ID_MENU_EDIT_COPY   (GUI_ID_USER + 12)
#define ID_MENU_EDIT_PASTE  (GUI_ID_USER + 13)
#define ID_MENU_EDIT_DELETE (GUI_ID_USER + 14)
#define ID_MENU_HELP_ABOUT  (GUI_ID_USER + 15)

static WM_HWIN _CreateMenu(WM_HWIN hParent);

static void CbWin(WM_MESSAGE *pMsg)
{
    WM_HWIN hWin = pMsg->hWin;
    MENU_MSG_DATA * pData;

    switch(pMsg->MsgId)
    {
        case WM_PAINT:
        {
            GUI_Clear();
            //GUI_SetFont(&GUI_FontComic24B_ASCII);
            //GUI_SetColor(0xffffff);
            //GUI_DispStringHCenterAt("STM32F103", 64, 16);

            //GUI_SetFont(&GUI_Font16B_ASCII);
            //GUI_DispStringHCenterAt("emWin Demo", 64, 0);

            //GUI_SetFont(&GUI_Font16B_ASCII);
            //GUI_DispStringHCenterAt("Designed by yc", 64, 40);

            break;
        }

		case WM_MENU:
		{
			//pData = (MENU_MSG_DATA*)pMsg->Data.p;
			//switch(pData->MsgType)
			//{
			//	case MENU_ON_ITEMPRESSED:
			//		//
			//		// This message is send regardless of the item state is disabled or not
			//		//
			//		/*MENU_GetItem(pMsg->hWinSrc, pData->ItemId, &Data);
			//		if(Data.Flags & MENU_IF_DISABLED)
			//		{
			//		_MessageBox("The pressed item was disabled", "Message");
			//		}*/
			//		break;
			//	case MENU_ON_ITEMACTIVATE:
			//		//
			//		// This message is send on highlighting a menu item
			//		//
			//		//Index = pData->ItemId - ID_MENU_FILE_NEW;
			//		//if(Index >= 0)
			//	{
			//		//TEXT_SetText(_hText, _paDescription[pData->ItemId - ID_MENU_FILE_NEW]);
			//	}
			//	//else
			//		{
			//			//TEXT_SetText(_hText, "Ready");
			//		}
			//		break;
			//	case MENU_ON_ITEMSELECT:
			//		//
			//		// This message is send only if an enabled item has been selected
			//		//
			//		//sprintf(acBuffer, "ID of the selected\nitem is 0x%X", pData->ItemId);
			//		//_MessageBox(acBuffer, "Message");
			//		break;
			//}
			break;
		}
            

        case WM_CREATE:
        {
            /*BUTTON_Handle bHandle;
            bHandle = BUTTON_CreateAsChild(32, 40, 64, 24, hWin, GUI_ID_BUTTON0, BUTTON_CF_SHOW);
            BUTTON_SetText(bHandle, "Button");
            BUTTON_SetFont(bHandle, &GUI_Font20_ASCII);
            BUTTON_SetSkinClassic(bHandle);
            WM_SetFocus(bHandle);*/

            //MENU_CreateUser(0, 0, WINDOW_SIZEX, WINDOW_SIZEY, hWin, WM_CF_SHOW, MENU_CF_HORIZONTAL, MENU_ID)

            

            break;
        }

        default:
            WM_DefaultProc(pMsg);
    }
}

WM_HWIN WinMenuCreate(WM_HWIN hParent, void *p)
{
    /*WM_HWIN hWin = WM_CreateWindowAsChild(0, 0,
                                          WINDOW_SIZEX, WINDOW_SIZEY,
                                          hParent,
                                          WM_CF_MEMDEV | WM_CF_SHOW,
                                          CbWin,
                                          0);*/
	FRAMEWIN_Handle hWin;
    hWin = FRAMEWIN_CreateEx(0, 0, WINDOW_SIZEX, WINDOW_SIZEY, hParent, WM_CF_SHOW, FRAMEWIN_CF_ACTIVE, 0, "Application with menu", NULL);
    FRAMEWIN_SetTitleVis(hWin, 0);
    FRAMEWIN_SetSkinClassic(hWin);

	FRAMEWIN_SetBorderSize(hWin, 0);
	FRAMEWIN_SetClientColor(hWin, 0x000000);


    _CreateMenu(hWin);

    return hWin;
}


static void _AddMenuItem(MENU_Handle hMenu, MENU_Handle hSubmenu, const char* pText, U16 Id, U16 Flags)
{
    MENU_ITEM_DATA Item;

    Item.pText = pText;
    Item.hSubmenu = hSubmenu;
    Item.Flags = Flags;
    Item.Id = Id;
    MENU_AddItem(hMenu, &Item);
}


static WM_HWIN _CreateMenu(WM_HWIN hParent)
{
    MENU_Handle hMenu;
    MENU_Handle hMenuFile;
    MENU_Handle hMenuEdit;
    MENU_Handle hMenuHelp;

    //
    // Create main menu
    //
    hMenu = MENU_CreateEx(0, 0, 0, 0, WM_UNATTACHED, WM_CF_SHOW, MENU_CF_HORIZONTAL, ID_MENU);
    //
    // Create sub menus
    //
    hMenuFile = MENU_CreateEx(0, 0, 0, 0, hMenu, WM_CF_SHOW, MENU_CF_VERTICAL, 0);
    hMenuEdit = MENU_CreateEx(0, 0, 0, 0, hMenu, WM_CF_SHOW, MENU_CF_VERTICAL, 0);
    hMenuHelp = MENU_CreateEx(0, 0, 0, 0, hMenu, WM_CF_SHOW, MENU_CF_VERTICAL, 0);
    //hMenuRecent = MENU_CreateEx(0, 0, 0, 0, WM_UNATTACHED, 0, MENU_CF_VERTICAL, 0);
    //
    // Add menu items to menu 'Recent'
    //
    //_AddMenuItem(hMenuRecent, 0, "File 1", ID_MENU_RECENT_0, 0);
    //_AddMenuItem(hMenuRecent, 0, "File 2", ID_MENU_RECENT_1, 0);
    //_AddMenuItem(hMenuRecent, 0, "File 3", ID_MENU_RECENT_2, 0);
    //_AddMenuItem(hMenuRecent, 0, "File 4", ID_MENU_RECENT_3, 0);
    //
    // Add menu items to menu 'File'
    //
    _AddMenuItem(hMenuFile, 0, "New", ID_MENU_FILE_NEW, 0);
    _AddMenuItem(hMenuFile, 0, "Open", ID_MENU_FILE_OPEN, 0);
    //_AddMenuItem(hMenuFile, 0, "Close", ID_MENU_FILE_CLOSE, 0);
    //_AddMenuItem(hMenuFile, 0, "Exit", ID_MENU_FILE_EXIT, 0);
    //
    // Add menu items to menu 'Edit'
    //
    _AddMenuItem(hMenuEdit, 0, "Undo", ID_MENU_EDIT_UNDO, 0);
    _AddMenuItem(hMenuEdit, 0, "Redo", ID_MENU_EDIT_REDO, 0);
    _AddMenuItem(hMenuEdit, 0, "Copy", ID_MENU_EDIT_COPY, 0);
    //_AddMenuItem(hMenuEdit, 0, "Paste", ID_MENU_EDIT_PASTE, 0);
    //_AddMenuItem(hMenuEdit, 0, "Delete", ID_MENU_EDIT_DELETE, 0);
    //
    // Add menu items to menu 'Help'
    //
    _AddMenuItem(hMenuHelp, 0, "About", ID_MENU_HELP_ABOUT, 0);
    //
    // Add menu items to main menu
    //
    _AddMenuItem(hMenu, hMenuFile, "File", 0, 0);
    _AddMenuItem(hMenu, hMenuEdit, "Edit", 0, 0);
    _AddMenuItem(hMenu, hMenuHelp, "Help", 0, 0);

    MENU_SetSkinClassic(hMenu);
	MENU_SetDefaultEffect(&WIDGET_Effect_None);

    //MENU_SetDefaultBkColor(MENU_CI_ACTIVE_SUBMENU, 0x000000);
    //MENU_SetDefaultBkColor(MENU_CI_SELECTED, 0x000000);
    //MENU_SetDefaultBkColor(MENU_CI_ENABLED, 0x000000);
    //MENU_SetDefaultBkColor(MENU_CI_DISABLED, 0x000000);

    //MENU_SetDefaultTextColor(MENU_CI_ACTIVE_SUBMENU, 0x000000);
    //MENU_SetDefaultTextColor(MENU_CI_SELECTED, 0x000000);
    //MENU_SetDefaultTextColor(MENU_CI_ENABLED, 0xffffff);
    //MENU_SetDefaultTextColor(MENU_CI_DISABLED, 0xffffff);

    MENU_SetBkColor(hMenu, MENU_CI_ACTIVE_SUBMENU, 0xffffff);
    MENU_SetBkColor(hMenu, MENU_CI_SELECTED, 0xffffff);
    MENU_SetBkColor(hMenu, MENU_CI_ENABLED, 0x000000);
    MENU_SetBkColor(hMenu, MENU_CI_DISABLED, 0x000000);

    MENU_SetTextColor(hMenu, MENU_CI_ACTIVE_SUBMENU, 0x000000);
    MENU_SetTextColor(hMenu, MENU_CI_SELECTED, 0x000000);
    MENU_SetTextColor(hMenu, MENU_CI_ENABLED, 0xffffff);
    MENU_SetTextColor(hMenu, MENU_CI_DISABLED, 0xffffff);

	MENU_SetBorderSize(hMenu, MENU_BI_TOP, 1);
	MENU_SetBorderSize(hMenu, MENU_BI_BOTTOM, 2);

	MENU_SKINFLEX_PROPS prop;
	MENU_GetSkinFlexProps(&prop, MENU_SKINFLEX_PI_SELECTED);
	//prop.FrameColorSelH = 0x000000;
	//prop.FrameColorSelV = 0x000000;
	prop.BkColorV = 0x000000;
	prop.FrameColorV = 0x000000;
	prop.TextColor = 0xffffff;
	prop.aSelColorH[0] = 0x000000;
	prop.aSelColorH[1] = 0x000000;
	prop.aSelColorV[0] = 0x000000;
	prop.aSelColorV[1] = 0x000000;
	MENU_SetSkinFlexProps(&prop, MENU_SKINFLEX_PI_SELECTED);

    //
    // Attach menu to parent window
    //
    //MENU_Attach(hMenu, hParent, 0, 0, WINDOW_SIZEX, WINDOW_SIZEY, 0);

    WM_SetFocus(hMenu);
    FRAMEWIN_AddMenu(hParent, hMenu);
    return hMenu;
}
